layui.use(['table','form','layer','laydate','element'], function() {
	var table = layui.table, 
		table2=layui.table, 
		form = layui.form, 
		$ = layui.jquery,
		laydate = layui.laydate,
		element = layui.element;
   
	laydate.render({
	    elem: '#editbirthday'
	});
    
	/*动态加载班级下拉框*/
	form.on('select(selgradename)', function(data) {
		var schoolid = $("#selschool").val();
		var gradname=data.value;
		if(schoolid==00 || schoolid=='null' || schoolid==null){
			layer.msg("请选择学校");
			return;
		}
		if(gradname==00 || gradname=='null' || gradname==null){
			gradname="";
		}
		$("#classid").html("");
		$.ajax({
			type : 'get',
			url : '../sysclassmanagement/getclassbyschoolandgrade?schoolid=' + schoolid+'&gradename='+gradname,
			datatype : 'json',
			success : function(classdata) {
				if (classdata.code == 0) {
					var str = "<option value='00'>请选择班级</option>";
					for(var i = 0; i < classdata.count; i++){
						str += '<option value=' + classdata.data[i].classid + '>'+classdata.data[i].className +'</option>';
					}
					$("#classid").append(str);
					form.render();
				}
				else{
					layer.msg('没有对应的班级信息', {icon: 2});
					$("#classid").html("");
					form.render("select");
				}
			}
		});
	});

	/*点击查询学生信息*/
	$("#btnselstu").click(function(){
		var schoolid=$("#selschool").val();
		var gradename=$("#selgradename").val();
		var classid = $("#classid").val();
		var userName = $("#userName").val().trim();
		if(schoolid==00 || schoolid==null || schoolid=='null'){
			schoolid="";
		}
		if(gradename==00 || gradename==null || gradename=='null'){
			gradename="";
		}
		if(classid==00 || classid==null || classid=='null'){
			classid="";
		}
		table.render({
			id:'studentid',
			elem: '#tabeditstu',
			url: '../studentmanagement/getstudentwhere?idorrelname='+userName+'&classid='+classid+'&gradename='+gradename+"&schoolid="+schoolid,
			title: '学生数据表',
			height: 'full-130',
			skin: 'line',
			even: true,
			cols: [
				[{
					field: 'stuid',
					title: '账号',
					align: "center"
				}, {
					field: 'realName',
					title: '真实姓名',
					align: "center"
				}, {
					field: 'sex',
					title: '性别',
					align: "center"
				}, {
					field: 'className',
					title: '所属班级',
					align: "center"
				}, {
					field: 'gradename',
					title: '所属年级',
					align: "center"
				},{
					field: 'schoolName',
					title: '所属学校'	,
					align: "center"
				},{
					field: 'birthday',
					title: '出生日期',
					align: "center"
				}, {
					field: 'gradeYear',
					title: '入学年份'	,
					align: "center"
				},{
					title: '操作',
					align: "center",
					toolbar: '#barDemo',
					width:180
				}]
			],
			page: {
				layout: ['prev', 'page', 'next', 'skip', 'count', 'limit'],
				groups: 5,
				limit: 10,
				limits: [10, 20, 30, 40, 50],
				theme: '#1E9FFF',						
			}
		});
	});
	
	$("#downloadstupar").click(function(){
		var grade = $("#selgradename").val();
		var userName = $("#userName").val();
		var classid = $("#classid").val();
		var schoolid = $("#selschool").val();
		$.ajax({
			type: 'get',
			url: '../studentmanagement/downloadstupar?userName=' + userName + '&classid=' + classid + "&grade=" + grade + "&schoolid=" + schoolid,
			datatype: 'json',
			success: function(data) {
				if(data.code == 10001) {
					var link = document.createElement("a");
					link.href = data.msg;
					link.style = "visibility:hidden";
					link.download = data.ExcelName;
					document.body.appendChild(link);
					link.click();
					document.body.removeChild(link);										
				} else {
					layer.alert(data.msg, {icon: 2});
				}
			},
			error: function() {
				layer.alert('下载失败！', {icon: 2});
			}
		});
	}) 
	
	 //监听行工具事件
    table.on('tool(tabeditstu)', function(obj){
	    var data = obj.data;
	    /*点击查看学生信息详情*/
	    if(obj.event === 'btnselstudent'){
	    	var index=layer.open({
		        type: 1, 
		        title: '学生信息详情',
		        area: ['600px', '470px'],
		        shade: 0.8,
		        maxmin:true,
		        content: $('#studetail'),
		        cancel: function(){ 
				   $(".studetail-con").css("display","none");
				}
		    });
	    	layer.full(index);
	    	
	    	/*学生信息详情*/
	    	$("#laystuid").text(data.stuid);
		    $("#layrealname").text(data.realName);
		    $("#laysex").text(data.sex);
		    $("#layclass").text(data.className);
		    $("#laygrad").text(data.gradeYear);
		    
		    /*学生家长对应关系*/
	    	table2.render({
				elem: '#parentStus',
				url: '../sysparentmanagement/getparentstudent?stuid='+data.stuid,
				title: '家长学生数据表',
				skin: 'line',
				even: true,
				cols: [
					[{
						field: 'parUserName',
						align: "center",
						width:"20%",
						title: '家长账号'
					}, {
						field: 'parRealName',
						align: "center",
						width:"20%",
						title: '家长真实姓名'
					}, {
						field: 'relation',
						align: "center",
						width:"20%",
						title: '关系'
					}, 
					{
						field: 'phone',
						align: "center",
						width:"20%",
						title: '联系电话'
					},{
						field: 'stuRealName',
						align: "center",
						width:"20%",
						title: '学生姓名'
					}]
				],
			});
	    }
	    /*学生信息修改*/
	    else if(obj.event === 'btneditstudent'){
	    	var index=layer.open({
				type: 1, 
			    title: '修改学生信息',
		        area: ['830px', '95%'],
		        shade: 0.8,
		        maxmin: true,
		        content: $('#editstuFrom'),
		        cancel: function(){ 
				   $(".editstulayer-con").css("display","none");
				}
		    });
	    	layer.full(index);
	    	$("#editid").val(data.id);
		    $("#edituserName").val(data.stuid);
		    $("#editrealName").val(data.realName);
		    $("#seleditcla").val(data.gradename+data.className);
		    $("#editbirthday").val(data.birthday);
		    $(".sex").each(function(){
				if($.trim($(".sex")[0].value) == $.trim(data.sex)){
					$(".sex")[0].checked = true;
					form.render("radio");
				}
				if($.trim($(".sex")[1].value) == $.trim(data.sex)){
					$(".sex")[1].checked = true;
					form.render("radio");
				}
			});
	    }
	    /*学生信息删除*/
	    else if(obj.event === 'btndelstudent'){
	    	var stuid=data.id;
	    	layer.confirm('确定要删除么？', {
			  btn: ['确定','取消'],
			  icon:3
			}, function(){
				$.ajax({
	        		type: 'get',
	        		url: "../studentmanagement/deletestudent?stuid=" + stuid,
	        		dataType: 'json',
	        		success:function(data){
	        			if(data.code == 10001){
	        				layer.alert(data.msg, {
							    btn: ['知道了'],
							    icon:1
							}, function(){
								table.reload("studentid", {
					                where: {
					                	keyword:data.code=='10001'
					                }
					            });	
	                         	layer.closeAll();
							});		            				 
	        			}
	        			else{
	        				layer.alert(data.msg,2);
	        			}
	        		},
	        		error:function(){
	        			layer.msg("发生未知错误！！！");
	        		},
	        	});   
			}, function(){});
	    }
    });
	
	/*保存修改*/
    $("#btneditstu").click(function(){
    	var editid=$("#editid").val();
    	var editrealName=$("#editrealName").val();
    	var editsex=$("input[name='sex']:checked").val();
    	var editbirthday=$("#editbirthday").val();
		$.ajax({
			url: "../studentmanagement/updatestudent",  
	        type: "get", 
	        dataType: "json",  
	        data:{id:editid,realName:editrealName,sex:editsex,birthday:editbirthday},
	        success: function (getdata) {
    	          if (getdata.code == 10001) {
    	        	  layer.confirm(getdata.msg, {
						  btn: ['确定'],
						  icon:1
					  }, function(){
						 layer.closeAll();
						 table.reload("studentid", {
	    		              where: {
	    		               	keyword:getdata.code=='10001'
	    		              }
	              		  });	
					   });
    	          }else {  
    	        	  layer.alert("学生修改失败",2);
    	        	  return false;
    	          }
	        }
		});  
    });
});